package org.example.xtcs.anpes1;

import com.google.auto.service.AutoService;
/*import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;*/
import org.example.xtcs.annotate.XtcsEnum;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

/**
 * @ClassName: CustomAnnotationProcessor
 * @Description:
 * @Author: YourName
 * @Date: 2024/12/18
 */

//@SupportedAnnotationTypes("org.example.xtcs.annotate.XtcsEnum")
//@SupportedSourceVersion(SourceVersion.RELEASE_8)
//@AutoService(Processor.class)
public class XtcsEnumAnnotationProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        return false;
    }
    /*java.util.List<String> fieldNames = new ArrayList<>(Arrays.asList("xtcs1", "xtcs2"));
    private JavacTrees javacTrees; // 获取 JcTree
    private TreeMaker treeMaker; // 构建生成 jcTree
    private Names names;


    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        System.out.println("【XtcsEnum】编译注释处理器");
        javacTrees = JavacTrees.instance(processingEnv);// 语法树
        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
        this.treeMaker = TreeMaker.instance(context);
        this.names = Names.instance(context);
        super.init(processingEnv);
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(XtcsEnum.class)) {
            // 获取字段信息
            String className = element.getSimpleName().toString();
            TypeMirror fieldType = element.asType();
            System.out.println("类路径-------------" + fieldType.toString());
            JCTree tree = javacTrees.getTree(element);
            JCTree.JCClassDecl classDecl = (JCTree.JCClassDecl) tree;

            //修改类，改变编译结果文件 枚举类
            System.out.println("=============================111"+classDecl.toString());
            // 创建一个新的枚举常量的声明
            JCTree.JCExpression returnType;
            JCTree.JCExpression init;
            Name fName = names.fromString("XTCS");
            returnType = treeMaker.Ident(names.fromString(className));
            init = treeMaker.NewClass(null, List.<JCTree.JCExpression>nil(), treeMaker.Ident(names.fromString("XTCS")), List.<JCTree.JCExpression>nil(), null);
            JCTree.JCModifiers constantValueMods = treeMaker.Modifiers(Flags.ENUM);
            JCTree.JCVariableDecl jcVariableDecl = treeMaker.VarDef(constantValueMods, fName, returnType, init);
            System.out.println("=============================222"+jcVariableDecl.toString());
            classDecl.defs = classDecl.defs.append(jcVariableDecl);
            System.out.println("=============================33333"+classDecl.toString());
        }
        return true;
    }*/
}
